package com.spring.team_propaganda_system.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
//https://blog.csdn.net/violin256/article/details/106389322
@Aspect
@Component
public class AdminLogging {
    private static final Logger log = LoggerFactory.getLogger(AdminLogging.class);

    //定义切点 @Pointcut
    @Pointcut("execution(* com.spring.team_propaganda_system.controller.*.*(..))")
    public void logPoinCut() {
    }

    @Before("logPoinCut()")
    public void before(JoinPoint joinPoint){
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();//这个RequestContextHolder是Springmvc提供来获得请求的东西
        HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();

        // 记录下请求内容
        log.info("请求日志消息");
        log.info("################请求URL : " + request.getRequestURL().toString());
        log.info("################HTTP_METHOD请求方法 : " + request.getMethod());
        log.info("################IP : " + request.getRemoteAddr());
        log.info("################控制器参数 : " + Arrays.toString(joinPoint.getArgs()));

        log.info("################类方法CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
    }
    @After("logPoinCut()")
    public void after(){
        log.info("该请求结束日志");
    }

}